/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.operator;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;

/**
 * @author Administrator
 *
 */
public class PowerInfoSearchCondition extends BaseSearchCondition {
	

	private String orgCode;
	private String accountNo;
	private String realName;
	private String cardNo;
	private String mobile;
	private String roleType;
	private String[] agentId;//所属代理商
	private String opId;//当前操作员id
	private String currrentRoleType;//当前操作员角色类型
	private String currrentOrgCode;

	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getOrderPartOfHQL()
	 */
	@Override
	protected String getOrderPartOfHQL() {
		// TODO Auto-generated method stub
		
		return "   ";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getRowCountHQL()
	 */
	/*@Override
	protected String getRowCountHQL() {
		
		return " select count(*) from t_operators o where 1=1  and o.role_type<>'00'  ";
	}*/
	@Override
	protected String getRowCountHQL() {
		StringBuffer  sb= new StringBuffer();
		if(!currrentRoleType.trim().equals("00")){
			if(StringUtils.isEmpty(currrentOrgCode)){
				sb.append(" SELECT COUNT(1) FROM (");
				sb.append(" SELECT T.*, TI.INS_NAME FROM T_OPERATORS T, TBL_INS_INF TI ");
				sb.append("  WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1' AND T.STATUS IN ('00', '02')");
				sb.append(" START WITH ID =").append(opId);
				sb.append(" CONNECT BY PRIOR T.ID = T.SUPRI_ID )T   ");
				sb.append(" WHERE 1=1  and  T.role_type<>'15'  ");
			}else{
				if(currrentOrgCode.equals("48502000")){
					sb.append(" SELECT COUNT(1)");
					sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1'");
					sb.append(" AND T.STATUS IN('00','02') and t.role_type<>'15'   ");
				}else{
					sb.append(" SELECT COUNT(1) FROM (");
					sb.append(" SELECT T.*, TI.INS_NAME FROM T_OPERATORS T, TBL_INS_INF TI ");
					sb.append("  WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1' AND T.STATUS IN ('00', '02')");
					sb.append(" START WITH ID =").append(opId);
					sb.append(" CONNECT BY PRIOR T.ID = T.SUPRI_ID )T   ");
					sb.append(" WHERE 1=1  and  T.role_type<>'15'  ");
				}
			}
			
		}else{//系统管理员
			sb.append(" SELECT COUNT(1)");
			sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1'");
			sb.append(" AND T.STATUS IN('00','02') and t.role_type<>'15'   ");
		}
		return sb.toString();
	}
	@Override
	protected String getSearchHQL() {
		StringBuffer  sb= new StringBuffer();
		if(!currrentRoleType.trim().equals("00")){
			if(StringUtils.isEmpty(currrentOrgCode)){
				sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
				sb.append(" T.CARD_NO, T.STATUS,T.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
				sb.append(" FROM (SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,T.CARD_NO, T.STATUS,");
				sb.append(" T.VALID_START_DATE,T.VALID_END_DATE,T.DT_CREATE,TI.INS_NAME,T.ORG_CODE FROM T_OPERATORS T, TBL_INS_INF TI ");
				sb.append("  WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1' AND T.STATUS IN ('00', '02')");
				sb.append(" START WITH ID =").append(opId);
				sb.append(" CONNECT BY PRIOR T.ID = T.SUPRI_ID )T    ");
				sb.append(" WHERE 1=1 and  T.role_type<>'15' AND T.role_type<>'17' ");
			}else{
				if(currrentOrgCode.equals("48502000")){
					sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
					sb.append(" T.CARD_NO, T.STATUS,TI.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
					sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1'");
					sb.append(" AND T.STATUS IN('00','02') and  t.role_type<>'15' and  t.role_type<>'17'    ");
				}else{
					sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
					sb.append(" T.CARD_NO, T.STATUS,T.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
					sb.append(" FROM (SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,T.CARD_NO, T.STATUS,");
					sb.append(" T.VALID_START_DATE,T.VALID_END_DATE,T.DT_CREATE,TI.INS_NAME,T.ORG_CODE FROM T_OPERATORS T, TBL_INS_INF TI ");
					sb.append("  WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1' AND T.STATUS IN ('00', '02')");
					sb.append(" START WITH ID =").append(opId);
					sb.append(" CONNECT BY PRIOR T.ID = T.SUPRI_ID )T    ");
					sb.append(" WHERE 1=1 and  T.role_type<>'15' AND T.role_type<>'17' ");
				}
			}
		}else{//系统管理员
			sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
			sb.append(" T.CARD_NO, T.STATUS,TI.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
			sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1'");
			sb.append(" AND T.STATUS IN('00','02') and  t.role_type<>'15' and  t.role_type<>'17'    ");
		}
		return sb.toString();
	}
	/* (non-Javadoc)
	 * @see com.roc.page.BaseSearchCondition#getWhereHQLFieldBeanList()
	 */
	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();

		
		if (Constance.isNotEmpty(accountNo)) {
			filedBeanList.add(new WhereHQLFieldBean("T.ACCOUNT_NO", accountNo.trim()
					.trim(), "=", false));
		}

		if (Constance.isNotEmpty(realName)) {
			filedBeanList.add(new WhereHQLFieldBean("T.REAL_NAME", realName.trim(),
					"LIKE", false, "ALL"));
		}
		if (Constance.isNotEmpty(mobile)) {
			filedBeanList.add(new WhereHQLFieldBean("T.MOBILE", mobile.trim()
					.trim(), "=", false));
		}
		if (Constance.isNotEmpty(cardNo)) {
			filedBeanList.add(new WhereHQLFieldBean("T.CARD_NO", cardNo.trim()
					.trim(), "=", false));
		}
		
        
		if (Constance.isNotEmpty(orgCode)) {
				filedBeanList.add(new WhereHQLFieldBean("T.org_code", orgCode.trim()
						.trim(), "=", false));
		}
		
		if (Constance.isNotEmpty(roleType)) {
				if(!roleType.equals("99")){
					filedBeanList.add(new WhereHQLFieldBean("T.ROLE_TYPE", roleType, "=", false));
				}
		}
		/*if(agentId!=null&&agentId.length >0){//代理商
        	filedBeanList.add(new WhereHQLFieldBean("o.id IN("+Constance.getArrayContent(agentId)+")"));
        }*/

		return filedBeanList;
	}

	/**
	 * @return the accountNo
	 */
	public String getAccountNo() {
		return accountNo;
	}

	/**
	 * @param accountNo the accountNo to set
	 */
	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	/**
	 * @return the realName
	 */
	public String getRealName() {
		return realName;
	}

	/**
	 * @param realName the realName to set
	 */
	public void setRealName(String realName) {
		this.realName = realName;
	}

	/**
	 * @return the cardNo
	 */
	public String getCardNo() {
		return cardNo;
	}

	/**
	 * @param cardNo the cardNo to set
	 */
	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String[] getAgentId() {
		return agentId;
	}

	public void setAgentId(String[] agentId) {
		this.agentId = agentId;
	}

	public String getOpId() {
		return opId;
	}

	public void setOpId(String opId) {
		this.opId = opId;
	}

	public String getCurrrentRoleType() {
		return currrentRoleType;
	}

	public void setCurrrentRoleType(String currrentRoleType) {
		this.currrentRoleType = currrentRoleType;
	}

	public String getCurrrentOrgCode() {
		return currrentOrgCode;
	}

	public void setCurrrentOrgCode(String currrentOrgCode) {
		this.currrentOrgCode = currrentOrgCode;
	}
	
}
